package com.chaodriod.common.fragment;

import android.animation.Animator;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;

import com.chaodriod.common.utils.ALog;

/**
 * Created by CHEN on 2018/1/16.
 */

public class LoggerFragment extends Fragment {

    public void log(String msg) {
        ALog.e(msg);
    }

    /********************************主要生命周期********************************/
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        log("onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        log("onCreate");
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //子类不会调用 Super()
        log("onCreateView");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        log("onViewCreated");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        log("onActivityCreated");
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        log("onViewStateRestored");
    }

    @Override
    public void onStart() {
        super.onStart();
        log("onStart");
    }

    /**
     * 每add一个childFragment，就会回调onAttachFragment一次
     * 如果初始化时添加childFragment，会在onStart和onResume之间回调onAttachFragment
     */
    @Override
    public void onAttachFragment(Fragment childFragment) {
        super.onAttachFragment(childFragment);
        log("onAttachFragment: " + childFragment);
    }

    @Override
    public void onResume() {
        super.onResume();
        log("onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        log("onPause");
    }

    /**
     * 进入后台的时候会回调onSaveInstanceState，在onPause和onStop之间
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        log("onSaveInstanceState");
    }

    @Override
    public void onStop() {
        super.onStop();
        log("onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        log("onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        log("onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        log("onDetach");
    }

    /**
     * 当调用Fragment的hide()和show()方法时，会回调onHiddenChanged,不会回调onStop()等生命周期方法
     * 而新的Fragment在创建时是不会回调onHiddenChanged()，
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        log("onHiddenChanged_hidden: " + hidden);
    }

    /**
     * 在清单文件中Activity标签下可以指定参数android：ConfigChanges，用于捕获手机状态的改变。
     * 当所指定属性(Configuration Changes)发生改变时，会调用onConfigurationChanged()
     * 如果改变的属性没有在清单文件中指定就会销毁当前Activity，重新创建一个
     * 具体属性如下：
     * "mcc"	        国际移动用户识别码所属国家代号改变了-----  sim被侦测到了，去更新mcc
     * mcc是移动用户所属国家代号
     * "mnc"	        国际移动用户识别码的移动网号码改变了------ sim被侦测到了，去更新mnc
     * MNC是移动网号码，最多由两位数字组成，用于识别移动用户所归属的移动通信网
     * "locale"	        地址改变了-----用户选择了一个新的语言会显示出来
     * "touchscreen"	触摸屏是改变了------通常是不会发生的
     * "keyboard"	    键盘发生了改变----例如用户用了外部的键盘
     * "keyboardHidden"	键盘的可用性发生了改变
     * "navigation"	    导航发生了变化-----通常也不会发生
     * "screenLayout"	屏幕的显示发生了变化------不同的显示被激活
     * "fontScale"	    字体比例发生了变化----选择了不同的全局字体
     * "uiMode"	        用户的模式发生了变化
     * "orientation"	屏幕方向改变了
     * "screenSize"	    屏幕大小改变了，屏幕旋转也会导致screenSize改变
     * "smallestScreenSize"	屏幕的物理大小改变了，如：连接到一个外部的屏幕上
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        log("onConfigurationChanged: " + newConfig);
    }

    @Override
    public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
        super.onInflate(context, attrs, savedInstanceState);
        log("onInflate");
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        log("onLowMemory");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        log("onActivityResult: requestCode = " + requestCode + ", resultCode = " + resultCode + ", data = " + data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        log("onRequestPermissionsResult");
    }

    /**
     * Called when a fragment loads an animation.
     * 包括转场动画
     */
    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        log("onCreateAnimation");
        return super.onCreateAnimation(transit, enter, nextAnim);
    }

    /**
     * Called when a fragment loads an animator.
     * 包括转场动画
     */
    @Override
    public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
        log("onCreateAnimator");
        return super.onCreateAnimator(transit, enter, nextAnim);
    }

    /**
     * 多窗口模式
     */
    @Override
    public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
        super.onMultiWindowModeChanged(isInMultiWindowMode);
        log("onMultiWindowModeChanged");
    }

    /**
     * 画中画模式
     */
    @Override
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
        super.onPictureInPictureModeChanged(isInPictureInPictureMode);
        log("onPictureInPictureModeChanged");
    }
}
